#!/bin/sh
# PCP QA Test No. 1774
# test pmlogctl and the pmlogger_farm service
#
# Copyright (c) 2020 Red Hat.  All Rights Reserved.
#

if [ $# -eq 0 ]
then
    seq=`basename $0`
    echo "QA output created by $seq"
else
    # use $seq from caller, unless not set
    [ -n "$seq" ] || seq=`basename $0`
    echo "QA output created by `basename $0` $*"
fi

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

_cleanup()
{
    cd $here
    $sudo pmlogctl destroy $remote
    $sudo rm -fr $PCP_ARCHIVE_DIR/$remote
    $sudo rm -rf $tmp $tmp.*
}

trap "rm -f $tmp.*" 0
[ "$PCPQA_SYSTEMD" = yes ] || _notrun "systemctl not installed or not active"
[ -f $PCP_SYSTEMDUNIT_DIR/pmlogger_farm.service ] || \
    _notrun "$PCP_SYSTEMDUNIT_DIR/pmlogger_farm.service not found"

# need a remote pmcd
remote=`./getpmcdhosts -v 'pcp>=4' -L -n 1 2>$tmp.err`
[ -z "$remote" ] && _notrun "`cat $tmp.err`"
remote=`echo $remote | sed -e 's/\..*$//'`

[ -d $PCP_ARCHIVE_DIR/$remote ] && \
    _notrun $PCP_ARCHIVE_DIR/$remote already exists, will not overwrite

# normally we would enable it, but for now (pcp-5.3.5) pmlogger_farm
# is WantedBy=pmlogger.service and so we expect it to be enabled
systemctl is-enabled pmlogger_farm >/dev/null 2>&1 || \
    _notrun pmlogger_farm.service is not enabled

status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

echo "remote=$remote" >>$seq_full

echo == create pmlogger for remote pmcd using pmlogctl | tee -a $seq_full
$sudo pmlogctl -C '-V -V' create $remote
remotepid=`pmlogctl status | tee -a $seq_full | $PCP_AWK_PROG '$1 == "'"$remote"'" {print $4}'`
echo "remotepid=$remotepid" >>$seq_full
[ -f  $PCP_ARCHIVE_DIR/pmlogger_check.log ] \
    && cat $PCP_ARCHIVE_DIR/pmlogger_check.log >>$seq_full

echo === check pmlogctl thinks it is running
remotestate=`pmlogctl status | tee -a $seq_full | $PCP_AWK_PROG '$1 == "'"$remote"'" {print $5}'`
echo "remotestate=$remotestate" >>$seq_full
if [ "$remotestate" != running ]; then
    echo FAILED to start pmlogger for host $remote
    exit
fi

echo == kill it with SIGTERM | tee -a $seq_full
$sudo kill -TERM $remotepid >/dev/null 2>&1
sleep 2

echo == check it died | tee -a $seq_full
remotestate=`pmlogctl status | tee -a $seq_full | $PCP_AWK_PROG '$1 == "'"$remote"'" {print $5}'`
echo "remotestate=$remotestate" >>$seq_full
if [ "$remotestate" != dead ]; then
    echo "FAILED to kill pmlogger (pid=$remotepid) for host $remote"
    $PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep -E '[P]PID)|/[p]mlogger( |$)' >>$seq_full
    exit
fi

echo == use pmlogctl check to restart it | tee -a $seq_full
$sudo pmlogctl check

echo == get the pid and state of the new pmlogger | tee -a $seq_full
remotepid=`pmlogctl status | tee -a $seq_full | $PCP_AWK_PROG '$1 == "'"$remote"'" {print $4}'`
echo "remotepid=$remotepid" >>$seq_full
remotestate=`pmlogctl status | tee -a $seq_full | $PCP_AWK_PROG '$1 == "'"$remote"'" {print $5}'`
echo "remotestate=$remotestate" >>$seq_full
if [ "$remotestate" != running ]; then
    echo FAILED to restart dead pmlogger for host $remote
    $PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep -E '[P]PID|/[p]mlogger( |$)' >>$seq_full
    exit
fi

echo == migrate it to the pmlogger_farm | tee -a $seq_full
$sudo pmlogctl --migrate check $remote

echo == check it is now in the pmlogger_farm service | tee -a $seq_full
if systemctl status pmlogger_farm 2>&1 | grep -q -s $remotepid; then
    echo OK all good
else
    exit
fi

# success, all done
status=0
exit
